#include "QSPrecompiledHeaders.h"
#include "QSOgreRenderSystem.h"

#include <Ogre.h>

using namespace QS;

void OgreRenderSystem::initialize()
{
	if (mRoot)
		//already initialized
		return;

	mRoot = new Ogre::Root("");

	//load Ogre plugins
	const QS::String renderSystem = "RenderSystem_Direct3D9";

#ifndef NDEBUG
	mRoot->loadPlugin(renderSystem + "_d");
#else
	mRoot->loadPlugin(renderSystem);
#endif

	if (!mRoot->showConfigDialog())
		exit(0);

	//Ogre initialization
	mRoot->initialise(true);
	mWindow = mRoot->getAutoCreatedWindow();
			
	//scene manager
	mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
	//viewport
	mWindow->addViewport(mSceneMgr->createCamera("dummy"));	
}

void OgreRenderSystem::shutdown()
{
	mRoot->shutdown();
	delete mRoot;

	mRoot = 0;
	mWindow = 0;
	mSceneMgr = 0;
}

void OgreRenderSystem::update()
{
	Ogre::WindowEventUtilities::messagePump(); 
	mRoot->renderOneFrame();
}

size_t OgreRenderSystem::getWindowHandle()
{
	size_t handle;
	mWindow->getCustomAttribute("WINDOW", &handle);
	return handle;
}

bool OgreRenderSystem::isRenderWindowClosed() const
{
	return mWindow->isClosed();
}